package com.sy.server.controller;

import com.sy.server.model.User;
import com.sy.server.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/user")
@CrossOrigin
public class UserController {
    @Autowired
    private IUserService service;

    @RequestMapping(value = "/existUser/{userName}",method = RequestMethod.GET)
    public ResponseEntity<Boolean> existUser(@PathVariable String userName){
        boolean flag=service.existUser(userName);
        return new ResponseEntity<>(flag,HttpStatus.OK);
    }

    @RequestMapping(value = "/getUserNameByCurrentIp",method = RequestMethod.GET)
    public ResponseEntity<String> getUserNameByCurrentIp(){
        String userName=service.getUserNameByCurrentIp();
        return new ResponseEntity<>(userName,HttpStatus.OK);
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ResponseEntity<Void> login(@RequestBody User user){
        int flag=service.login(user);
        switch (flag){
            case 0:
                return new ResponseEntity<>(HttpStatus.OK);
            case 1:
                return new ResponseEntity<>(HttpStatus.NOT_FOUND);
            case 2:
                return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
            default:
                return new ResponseEntity<>(HttpStatus.SERVICE_UNAVAILABLE);
        }
    }

    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    public ResponseEntity<Void> logout(){
        service.logout();
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @RequestMapping(value = "/reg",method = RequestMethod.POST)
    public ResponseEntity<Void> reg(@RequestBody User user){
        service.reg(user);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
